﻿using System;
using System.Collections.Generic;
using System.Xml;

namespace XML读写
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            BookWriter wt = new BookWriter();
            wt.writeXml();

            BookReader brd = new BookReader();
            brd.readXml();

            Console.ReadKey();
        }
    }
    

    public class XmlUtil
    {
        String _path;
        XmlDocument _doc;

        public String Path
        {
            get => _path;
            set => _path = value;
        }

        public XmlDocument Doc
        {
            get => _doc;
        }

        public XmlUtil()
        {

        }

        public void init()
        {
            if (Path != null && Path.Length > 1)
            {
                _doc = new XmlDocument();
                _doc.Load(Path);
            }
            else
            {
                throw new Exception("先设置xml路径");
            }
        }
    }

    public abstract class WriteUtil : XmlUtil,WriteAble
    {
        protected abstract void write();

        public void writeXml()
        {
            init();
            write();
        }
    }

    public abstract class ReadUtil:XmlUtil,ReadAble
    {
        protected abstract void read();

        public void readXml()
        {
            init();
            read();
        }

    }

    
    public interface WriteAble
    {
        void writeXml();
    }

    public interface ReadAble
    {
        void readXml();
    }

    public class BookWriter : WriteUtil
    {
        public BookWriter()
        {
            this.Path = @"..\..\Book.xml";
        }

        protected override void write()
        {

            //指定一个节点
            XmlNode root = Doc.SelectSingleNode("bookstore");

            XmlElement element = Doc.CreateElement("book");
            element.SetAttribute("Type", "必修课");
            element.SetAttribute("ISBN", "7-111-19149-15");

            XmlElement title = Doc.CreateElement("title");
            title.InnerText = "古文观止";
            XmlElement author = Doc.CreateElement("author");
            author.InnerText = "叶静";
            XmlElement price = Doc.CreateElement("price");
            price.InnerText = "45";

            element.AppendChild(title);
            element.AppendChild(author);
            element.AppendChild(price);

            root.AppendChild(element);

            // 保存
            Doc.Save(Path);

        }
    }

    public class BookReader : ReadUtil
    {
        public BookReader()
        {
            this.Path = @"..\..\Book.xml";
        }

        protected override void read()
        {
            XmlNode bookStore = Doc.SelectSingleNode("bookstore");
            XmlNodeList bookStoreChildNodes = bookStore.ChildNodes;

            List<BookModel> bookModels = new List<BookModel>();

            foreach (var childNode in bookStoreChildNodes)
            {
                if (childNode is XmlComment)
                {
                    XmlComment comment = (XmlComment)childNode;
                    // Console.WriteLine(comment.InnerText);
                }
                else if (childNode is XmlElement)
                {
                    BookModel bookModel = new BookModel();
                    XmlElement element = (XmlElement)childNode;
                    string elementName = element.Name;
                    XmlAttributeCollection attributes = element.Attributes;
                    // Console.WriteLine(elementName);

                    foreach (XmlAttribute attribute in attributes)
                    {
                        string attributeName = attribute.Name;
                        if (attributeName.Equals("Type"))
                        {
                            bookModel.BookType = attribute.Value;
                        }
                        else if (attributeName.Equals("ISBN"))
                        {
                            bookModel.BookISBN = attribute.Value;
                        }
                        // Console.WriteLine("{0},{1}",attributeName,attribute.Value);
                    }

                    XmlNodeList childNodes = element.ChildNodes;
                    foreach (XmlNode node in childNodes)
                    {
                        // Console.WriteLine("{0}{1}",node.Name,node.InnerText);

                        string nodeName = node.Name;
                        if (nodeName.Equals("title"))
                        {
                            bookModel.BookName = node.InnerText;
                        }
                        else if (nodeName.Equals("author"))
                        {
                            bookModel.BookAuthor = node.InnerText;
                        }
                        else if (nodeName.Equals("price"))
                        {
                            bookModel.BookPrice = Convert.ToDouble(node.InnerText);
                        }
                    }

                    bookModels.Add(bookModel);
                }
            }

            foreach (BookModel bookModel in bookModels)
            {
                Console.WriteLine(bookModel);
            }

            Console.ReadKey();
        }
    }


    public class BookModel
    {
        public BookModel()
        {
        }
        /// <summary>
        /// 所对应的课程类型
        /// </summary>
        private string bookType;

        public string BookType
        {
            get { return bookType; }
            set { bookType = value; }
        }

        /// <summary>
        /// 书所对应的ISBN号
        /// </summary>
        private string bookISBN;

        public string BookISBN
        {
            get { return bookISBN; }
            set { bookISBN = value; }
        }

        /// <summary>
        /// 书名
        /// </summary>
        private string bookName;
        public string BookName
        {
            get { return bookName; }
            set { bookName = value; }
        }

        /// <summary>
        /// 作者
        /// </summary>
        private string bookAuthor;

        public string BookAuthor
        {
            get { return bookAuthor; }
            set { bookAuthor = value; }
        }

        /// <summary>
        /// 价格
        /// </summary>
        private double bookPrice;

        public double BookPrice
        {
            get { return bookPrice; }
            set { bookPrice = value; }
        }

        public override string ToString()
        {
            return string.Format("{0},{1},{2}", bookName, bookAuthor, bookPrice);
        }
    }

}